<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/24
 * Time: 20:02
 */

namespace App\Http\Controllers;




use App\Service\DataReturnService;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Common\Base;
use Illuminate\Support\Facades\Validator;

class ActivityController extends Controller
{
    /**
     * @Notes:获取活动列表
     * @param Request $request
     * @return array
     * @author: LiBaoJin
     * @Time:
     */
    public function get_activity(Request$request){
        $postData  = ['token' => $request->post('token')];
        $validator = Validator::make($postData,
            [
                'token'         => 'required|string',
            ]);
        if ($validator->fails()) {
            return DataReturnService::returnError(1000,'验证错误！',$validator->errors());
        }
        $userInfo = Base::getUserByToken($postData['token']);
        if($userInfo['code'] != '666'){
            return $userInfo;
        }
        $user   = $userInfo['data'];
        try{
            $avtivity_list = DB::table('activity')
                ->where(['business_id'=>$user['business_id'],'state'=>0])
               ->select('id','business_id','activity_picture_url','content','product_ids','odinal')
                ->orderBy('odinal','asc')
                ->get()
                ->toarray();
            $avtivity_list = array_map('get_object_vars', $avtivity_list);
            if(empty($avtivity_list)){
                return DataReturnService::returnOk([''],'获取商品活动成功');
            }
            foreach($avtivity_list as $k=>$v){
                $product_ids = json_decode($v['product_ids']);
                $product_list = Db::table('product as p')
                    ->leftJoin('product_fineness as pf', 'p.product_condition','=','pf.id') //商品成色
                    ->leftJoin('product_brand_series as  pbs', 'p.systematics_id','=','pbs.id')//系列
                    ->leftJoin('product_brand as  pb', 'p.brand_id','=','pb.id')//品牌
                    ->whereIn('p.id',$product_ids)
                    ->select('p.id','p.pic_url','p.product_sn','p.full_name','p.product_name','p.original_price','p.price','p.peer_price','p.activity_price','p.market_price','pf.value','pbs.title as series_name','pb.title_all as brand_name')
                    ->get()
                    ->toarray();
                $product_list = array_map('get_object_vars', $product_list);
                foreach ($product_list as $key => $value) {
                    if ($value['market_price']) {
                        if ($value['activity_price']) {
                            $product_list[$key]['rebate'] = round(($value['activity_price'] / $value['market_price']) * 10, 1);
                        } elseif ($value['price']) {
                            $product_list[$key]['rebate'] = round(($value['price'] / $value['market_price']) * 10, 1);
                        } else {
                            $product_list[$key]['rebate'] = 0;
                        }
                    } else {
                        $product_list[$key]['rebate'] = 0;
                    }
                }
                $avtivity_list[$k]['product_list']=$product_list;
                $avtivity_list[$k]['content'] = htmlspecialchars_decode($avtivity_list[$k]['content']);
            }


            return DataReturnService::returnOk($avtivity_list,'获取商品活动列表');
        }catch(\Exception$e){
            return DataReturnService::returnError(-1,'获取活动失败'.$e);
        }
    }
    /**
     * @Notes:活动商品详情
     * @param Request $request
     * @return array
     * @author: LiBaoJin
     * @Time:
     */
    public function get_activity_detail(Request$request){
        $postData  = $request->post();
        $validator = Validator::make($postData,
            [
                'token'         => 'required|string',
                'id'     => 'required',
            ],
            [
                'token.required'        => 'token获取失败',
                'id.required'    => '商品id获取失败'
            ]);


        if ($validator->fails()) {
            return DRS::returnError(1000,'验证错误！',$validator->errors()->first());
        }
        try{
            $avtivity_list = Db::table('activity')
                ->where(['id'=>$postData['id'],'state'=>0])
                ->select('id','business_id','activity_picture_url','product_ids','odinal','content','main_picture_url','all_product_ids')
                ->get()
                ->toarray();
            if(empty($avtivity_list)){
                return DataReturnService::returnError(-1,'获取商品活动失败');
            }
            $avtivity_list =  array_map('get_object_vars', $avtivity_list);
            $avtivity_list[0]['content'] = htmlspecialchars_decode($avtivity_list[0]['content']);
            $product_ids = json_decode($avtivity_list[0]['all_product_ids'],true);
            $product_list = Db::table('product as p')
                ->leftJoin('product_fineness as pf', 'p.product_condition','=','pf.id') //商品成色
                ->leftJoin('product_brand_series as pbs', 'p.systematics_id','=','pbs.id')//系列
                ->leftJoin('product_brand as pb', 'p.brand_id','=','pb.id')//品牌
                ->whereIn('p.id',$product_ids)
                ->select('p.id','p.pic_url','p.product_sn','p.full_name','p.product_name','p.original_price','p.price','p.peer_price','p.activity_price','p.market_price','pf.value','pbs.title as series_name','pb.title_all as brand_name')
                ->get()
            ->toarray();
            $product_list = array_map('get_object_vars', $product_list);
            foreach ($product_list as $k => $v) {
                if ($v['market_price']) {
                    if ($v['activity_price']) {
                        $product_list[$k]['rebate'] = round(($v['activity_price'] / $v['market_price']) * 10, 1);
                    } elseif ($v['price']) {
                        $product_list[$k]['rebate'] = round(($v['price'] / $v['market_price']) * 10, 1);
                    } else {
                        $product_list[$k]['rebate'] = 0;
                    }
                } else {
                    $product_list[$k]['rebate'] = 0;
                }
            }
            $avtivity_list[0]['product_list']=$product_list;
            return DataReturnService::returnOk($avtivity_list,'获取商品活动列表');
        }catch(\Exception$e){
            return DataReturnService::returnError(-1,'获取活动失败'.$e);
        }
    }
}